subroutine rtraj(ss,step,y)
use var
use type
use parallel
implicit none
include 'mpif.h'
integer  :: ss,step,y,ioerr = 0, iout, iout2, i, mm, step_l
character*20 :: string

Escale = Elastic_L1 * nematic_l * 1.0d-9 ! (J/m)*(nm)*(10^-9)

iout = 200 + ID + 1
iout2 = 300 + ID + 1
mm = int(traj(1)/sample)

if (traj(2) == 0) then
if (ID == master) then
   do
   read(iout,IOSTAT=ioerr)string
   if (ioerr == 0) then
      read(iout)frame,ss,step,y
      read(iout)ISEED  
      read(iout)trj_E
      read(iout)NP_position
      read(iout)q
      write(900,'(I5,2X,4(f15.6),2(f20.6))')ss,trj_E(:,1),Escale*trj_E(5,1)/kbT
        
      write(999,*)number_NP
      write(999,*)
      Do i=1,number_NP
         write(999,*)"O",NP_position(:,i)
      enddo
      Energy_sycle(ss) = trj_E(3,1)
   else
      exit
   endif
   enddo
   q = q0 ! to make sure initial configuration of master is the same as other nodes
write(*,*)"Master READ THE TRAJ FILE"
write(*,*)"FRAME",frame,"CYCLE",ss,"STEP",step
write(222,*)"Master READ THE TRAJ FILE"
write(222,*)"FRAME",frame,"CYCLE",ss,"STEP",step
endif
CALL MPI_BCAST(Energy_sycle, sycle, MPI_DOUBLE_PRECISION, master, MPI_COMM_WORLD, IERR)
CALL MPI_BCAST(NP_position, 3*number_NP, MPI_INTEGER, master, MPI_COMM_WORLD, IERR)
CALL MPI_BCAST(ss, 1, MPI_INTEGER, master, MPI_COMM_WORLD, IERR)

else
   do
   read(iout,IOSTAT=ioerr)string
   if (ioerr == 0) then
      read(iout)frame,ss,step,y
      read(iout)ISEED  
      read(iout)trj_E
      read(iout)NP_position
      read(iout)q
      if (frame == 0) then
         write(iout2,'(I8,2X,4(f15.6),2(f20.6))')step,trj_E(:,1),Escale*trj_E(5,1)/kbT
      elseif (step == 0) then
         Do i=1,mm
            if (trj_E(1,i)/=0) then
               write(iout2,'(I8,2X,4(f15.6),2(f20.6))')step_l+i*sample,trj_E(:,i),Escale*trj_E(5,i)/kbT
            else
               step = step_l+(i-1)*sample
               exit
            endif
         enddo
      else
         Do i=1,mm
            if (trj_E(1,i)/=0) then
               write(iout2,'(I8,2X,4(f15.6),2(f20.6))')step_l+i*sample,trj_E(:,i),Escale*trj_E(5,i)/kbT
            else
               step = step_l+(i-1)*sample
               exit
            endif
         enddo
      endif
   else
      exit
   endif
   step_l = step
   trj_E = 0.d0
   enddo
write(*,*)"READ THE TRAJ FILE"
write(*,*)"FRAME",frame,"CYCLE",ss,"STEP",step
write(222,*)"READ THE TRAJ FILE"
write(222,*)"FRAME",frame,"CYCLE",ss,"STEP",step
endif
call RANDOM_SEED
!iseed(1:2) = iseed(1:2) 
call RANDOM_SEED(put = iseed(1:2))

frame = frame + 1
end subroutine

